package com.mxprojects.admin
{
	import com.mxprojects.constructors.ButtonSmall;
	import com.mxprojects.constructors.PopupWindow;
	import com.mxprojects.gui.ConfirmDialog;
	import com.mxprojects.gui.PostOptions;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Posts;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class AdminPosts extends MovieClip
	{
		private const ADMIN_SCRIPT:String = "admin-frontend.php";
		
		private var deleteButton:ButtonSmall;
		private var postOptions:PostOptions;
		private var backgroundMiddle:int;
		private var dialog:PopupWindow;
		private var postID:int;
		private var authorID:int;
		
		public function AdminPosts() {}
		
		public function display(postID:int, authorID:int):void {
			//let everyone know who thier parent is
			postOptions = parent as PostOptions;
			
			//set the shared vars
			this.postID = postID;
			this.authorID = authorID;
			
			//setup the middle line so we don't have to do it all the time
			backgroundMiddle = postOptions.background.height/2;
			
			//handle the buttons
			createDeleteButton();	
		}
		
		private function createDeleteButton():void {
			deleteButton = new ButtonSmall("<span class=\"buttonAdminText\">" + Language.main(Word.DELETE) + "</span>", deletePost);
			deleteButton.y = Math.round(backgroundMiddle - deleteButton.height/2);
			addChild(deleteButton);
		}
		
		private function deletePost(event:MouseEvent):void {
			//before we send off the request to delete let's ask them if they are sure
			ConfirmDialog.confirm(Language.main(Word.DELETE_CONFIRM), Language.main(Word.DELETE, true), confirmDeletePost);
		}
		
		private function confirmDeletePost(event:MouseEvent = null):void {
			//delete the post for real
			var dataToSend:Object = new Object();
			dataToSend.forumID = Posts.postData.forumID;
			dataToSend.threadID = Posts.postData.threadID;
			dataToSend.postID = postID;
			dataToSend.action = AdminActions.DELETE_POST;
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, deletePostSuccess, deleteButton, ADMIN_SCRIPT);
		}
		
		private function deletePostSuccess(returnData:Object):void {
			//if there are still posts left then refresh the thread otherwise go up a level
			if(returnData.viewPost == "true"){
				//just refresh the current thread
				Posts.load(Posts.postData.threadID, Posts.postData.currentPage);
			}else{
				//go up to the threads view
				DeepLinking.setLink(Word.THREADS, DeepLinking.createLinkArray(Posts.postData.forumID));
			}
		}
		
	}
}